package me.thinkjet.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import me.thinkjet.model.*;
import me.thinkjet.service.SysconfigKey;
import me.thinkjet.service.SysconfigService;
import me.thinkjet.service.TagService;
import me.thinkjet.service.UploadService;
import me.thinkjet.utils.DateTimeStampKit;

import java.util.List;

/**
 * CommonController
 */
@ControllerBind(controllerKey = "/", viewPath = "")
public class IndexController extends Controller {
	@Before(CacheInterceptor.class)
	@CacheName("index")
	public void index() {
		String index_page = SysconfigService.getValue(SysconfigKey.INDEX_PAGE_DIRCT);
		if (StringKit.notBlank(index_page)) {
			redirect(index_page);
			return;
		} else {
			/* Activity */
			List<Activity> activities = Activity.dao.findByCache("index",
					"activity-index", "select * from activity limit 0,5");
			DateTimeStampKit.generalDateTimeStamp(activities, "date_time");
			setAttr("activities", activities);

			/* Job */
			List<Job> jobs = Job.dao.findByCache("index",
					"job-index", "select j.*,r.*,u.username,u.name " +
					"from job j left join job_record r on r.id=j.id " +
					"left join users u on u.id=j.author order by j.hot desc limit 0,10");
			DateTimeStampKit.generalDateTimeStamp(jobs, "createtime");
			setAttr("jobs", jobs);

			/* News */
			List<News> news = News.dao.findByCache("index",
					"news-index", "select * from news limit 0,10");
			DateTimeStampKit.generalDateTimeStamp(news, "subdate");
			setAttr("news", news);

			/* Blog */
			List<Blog> blogs = Blog.dao.findByCache("index",
					"blog-index", "select b.*,u.name from blog b left join users u " +
					"on u.id=b.author order by b.hot desc limit 0,10");
			DateTimeStampKit.generalDateTimeStamp(blogs, "createtime");
			setAttr("blogs", blogs);

			/*Discuss*/
			List<Discuss> discussList = Discuss.dao.findByCache("index",
					"discuss-index", "select d.*,r.*,u.name,u.username from discuss d " +
					"left join discuss_record r on r.discuss_id = d.id " +
					"left join users u on u.id=d.author order by d.hot desc limit 0,10");
			DateTimeStampKit.generalDateTimeStamp(discussList, "subdate");
			setAttr("discusses", discussList);

			/* Slider */
			List<Slider> sliders = Slider.dao.findByCache("index", "slider-index",
					"select * from slider limit 0,5");
			for (Slider s : sliders) {
				String thumb = UploadService.generalThumbUrl(s.getStr("img"), 159, 60);
				if (thumb != null) {
					s.put("thumb", thumb);
				} else {
					s.put("thumb", s.getStr("img"));
				}
			}
			setAttr("sliders", sliders);

			/* tag */
			setAttr("tags", TagService.getHotTagsList());

			/* Side_top_content */
			setAttr("side_top_content",
					SysconfigService.getValue(SysconfigKey.INDEX_PAGE_SIDER_TOP_CONTENT));

			/* Link */
			setAttr("links", Link.dao.findByCache("index", "link",
					"select * from link order by sort asc"));
		}
	}

	public void noie() {
	}
}